﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;

namespace DBTools.DBUpdate.Comm
{
    public static class ExClass
    {
        #region 格式化
        //字符串加单引号
        public static string QuotedStr(this string value)
        {
            return "'" + value + "'";
        }

        public static string ToLikeStr(this string value)
        {
            return "'%" + value + "%'";
        }

        //表列名转大写
        public static DataTable ToUpperDataTable(this DataTable value)
        {
            if (value != null)
            {
                for (int i = 0; i < value.Columns.Count; i++)
                {
                    value.Columns[i].ColumnName = value.Columns[i].ColumnName.ToUpper();
                }
                return value;
            }
            else
            {
                return null;
            }
        }


        public static DataSet ToUpperDataTable(this DataSet value)
        {
            if (value != null)
            {
                foreach (DataTable _TempDataTable in value.Tables)
                {
                    for (int i = 0; i < _TempDataTable.Columns.Count; i++)
                    {
                        _TempDataTable.Columns[i].ColumnName = _TempDataTable.Columns[i].ColumnName.ToUpper();
                    }
                }
                return value;
            }
            else
            {
                return null;
            }
        }
        #endregion



        #region  数据转换
        public static DateTime ToDateTime(this DateTime? Value)
        {
            return (DateTime)Value;
        }

        //获取当天23：59：59的时间 用于日期<=的条件
        public static DateTime ToDayEndTime(this DateTime value)
        {
            return new DateTime(value.Year, value.Month, value.Day, 23, 59, 59);
        }
        public static string ToNormalTime(this DateTime value)
        {
            return value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static decimal ToDecimal(this decimal? Value)
        {
            if (Value == null)
            {
                return 0;
            }
            else
            {
                return (decimal)Value;
            }
        }
        public static string ConvertToChinese(this decimal Value)
        {
            string s = Value.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
            return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString());
        }
        public static decimal ToRound(this decimal Value, int decimals)
        {
            return Math.Round(Value, decimals);
        }
        public static decimal ToIntRound(this decimal Value, int decimals)
        {
            decimal _Count = (decimal)Math.Pow(10, decimals);
            decimal _Result = (Int64)(Value * _Count);
            return (_Result / _Count);
            //return ((int)(Value * 10 ^ (decimals + 2))) / 10 ^ (decimals + 2);
        }

        //这里null返回多少有待商榷
        public static decimal ToInt(this int? Value)
        {
            return (int)Value;
        }
        public static float ToFloat(this float? Value)
        {
            if (Value == null)
            {
                return 0;
            }
            else
            {
                return (float)Value;
            }
        }
        public static decimal ToRound(this float Value, int decimals)
        {
            return Math.Round((decimal)Value, decimals);
        }

        public static string ConvertToChinese(this int Value)
        {
            string s = Value.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
            return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString());
        }
        public static string ConvertToChinese(this float Value)
        {
            string s = Value.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
            return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString());
        }
 

        #endregion

        #region 基础扩展
        public static string Sub(this String Value, int SubLength = 7998)
        {
            if (Value == null)
            {
                return null;
            }
            if (Value.Length > SubLength)
            {
                return Value.Substring(0, SubLength);
            }
            else
            {
                return Value;
            }
        }
        #endregion



        #region 类扩展
        public static List<string> ClearItem(this List<string> _TempList, string ClearItem, string CreaedItem = "")
        {
            for (int i = 0; i < _TempList.Count; i++)
            {
                if (_TempList[i].Trim().ToUpper() == ClearItem.ToUpper())
                {
                    _TempList[i] = CreaedItem;
                }
            }
            return _TempList;
        }
        public static List<string> ToList(this string[] _Values)
        {
            return _Values.ToList<string>();
        }
        public static string ToInString(this List<string> _TempList, string SplitStr = "','")
        {
            return string.Join(SplitStr, _TempList);
        }
        public static string ToInString(this List<int> _TempList, string SplitStr = ",")
        {
            return string.Join(SplitStr, _TempList);
        }

        public static string GetLastDirectoryItemValue(this string _Path)
        {
            _Path = _Path.Replace("/", "\\");
            string[] _PathItems = _Path.Split('\\');
            if (_PathItems.Length <= 1)
            {
                return _Path;
            }
            else
            {
                if (_PathItems[_PathItems.Length - 1] == "")
                {
                    return _PathItems[_PathItems.Length - 2];
                }
                else
                {
                    return _PathItems[_PathItems.Length - 1];
                }
            }
        }
        #endregion
    }
}
